﻿Imports System.Reflection

Public Class clsMobileSettings

    Private _EnableAlerts As Boolean
    Public Property EnableAlerts() As Boolean
        Get
            Return _EnableAlerts
        End Get
        Set(ByVal value As Boolean)
            _EnableAlerts = value
            Me.Save()
        End Set
    End Property

    Private _Currency As String
    Public Property Currency() As String
        Get
            Return _Currency
        End Get
        Set(ByVal value As String)
            _Currency = value
            Me.Save()
        End Set
    End Property

    Private _Unit As String
    Public Property Unit() As String
        Get
            Return _Unit
        End Get
        Set(ByVal value As String)
            _Unit = value
            Me.Save()
        End Set
    End Property

    Private _LitrePer100KM As Double
    Public Property LitrePer100KM() As Double
        Get
            Return _LitrePer100KM
        End Get
        Set(ByVal value As Double)
            _LitrePer100KM = value
            Me.Save()
        End Set
    End Property

    Private _PriceOf20Litres As Double
    Public Property PriceOf20Litres() As Double
        Get
            Return _PriceOf20Litres
        End Get
        Set(ByVal value As Double)
            _PriceOf20Litres = value
            Me.Save()
        End Set
    End Property

    Private _SpeedAlert As Integer
    Public Property SpeedAlert() As Integer
        Get
            Return _SpeedAlert
        End Get
        Set(ByVal value As Integer)
            _SpeedAlert = value
            Me.Save()
        End Set
    End Property

    Private _SpeedAlert2 As Integer
    Public Property SpeedAlert2() As Integer
        Get
            Return _SpeedAlert2
        End Get
        Set(ByVal value As Integer)
            _SpeedAlert2 = value
            Me.Save()
        End Set
    End Property

    Private _KMLFolder As String
    Public Property KMLFolder() As String
        Get
            Return _KMLFolder
        End Get
        Set(ByVal value As String)
            _KMLFolder = value
            Me.Save()
        End Set
    End Property

    Private _LineWidth As Integer
    Public Property LineWidth() As Integer
        Get
            Return _LineWidth
        End Get
        Set(ByVal value As Integer)
            _LineWidth = value
            Me.Save()
        End Set
    End Property

    Private _LineColor As String
    Public Property LineColor() As String
        Get
            Return _LineColor
        End Get
        Set(ByVal value As String)
            _LineColor = value
            Me.Save()
        End Set
    End Property

    Private _RecordPositionEvery As String
    Public Property RecordPositionEvery() As String
        Get
            Return _RecordPositionEvery
        End Get
        Set(ByVal value As String)
            _RecordPositionEvery = value
            Me.Save()
        End Set
    End Property

    Private _FileTag As String
    Public Property FileTag() As String
        Get
            Return _FileTag
        End Get
        Set(ByVal value As String)
            _FileTag = value
            Me.Save()
        End Set
    End Property

    Private _Transparency As Integer
    Public Property Transparency() As Integer
        Get
            Return _Transparency
        End Get
        Set(ByVal value As Integer)
            _Transparency = value
            Me.Save()
        End Set
    End Property

    Private _AppPath As String
    Public ReadOnly Property AppPath() As String
        Get
            Dim asm As [Assembly] = [Assembly].GetExecutingAssembly()
            Return System.IO.Path.GetDirectoryName(asm.GetName().CodeBase)
        End Get
    End Property

    Private _SoundLanguage As String
    Public Property SoundLanguage() As String
        Get
            Return _SoundLanguage
        End Get
        Set(ByVal value As String)
            _SoundLanguage = value
        End Set
    End Property

    ''' <summary>
    ''' Save Application settings into Registry HKCU\Software\YAGPS
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Save()
        Try
            Dim objRegistry = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\YAGPS", True)
            objRegistry.SetValue("File Tag", Me.FileTag.ToString)
            objRegistry.SetValue("KML Folder", Me.KMLFolder.ToString)
            objRegistry.SetValue("Line Color", Me.LineColor.ToString)
            objRegistry.SetValue("Line Width", Me.LineWidth.ToString)
            objRegistry.SetValue("Record Position Every", Me.RecordPositionEvery.ToString)
            objRegistry.SetValue("Transparency", Me.Transparency.ToString)
            objRegistry.SetValue("Speed Alert", Me.SpeedAlert.ToString)
            objRegistry.SetValue("Speed Alert2", Me.SpeedAlert2.ToString)
            objRegistry.SetValue("Litre Per 100 KM", Me.LitrePer100KM.ToString)
            objRegistry.SetValue("Price Of 20 Litres", Me.PriceOf20Litres.ToString)
            objRegistry.SetValue("Sound Language", Me.SoundLanguage.ToString)
            objRegistry.SetValue("Unit", Me.Unit)
            objRegistry.SetValue("Currency", Me.Currency)
            objRegistry.SetValue("Enable Alerts", Me.EnableAlerts.ToString)
        Catch ex As Exception
            PushLog("Error in clsMobileSettings.Save: " & ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Load setting from registry
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub Load()
        Try
            Dim objRegistry = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\YAGPS")
            If objRegistry Is Nothing Then
                Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\YAGPS")
                objRegistry = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\YAGPS")
            End If
            Me._FileTag = objRegistry.GetValue("File Tag", "Tag")
            Me._KMLFolder = objRegistry.GetValue("KML Folder", "/")
            Me._LineColor = objRegistry.GetValue("Line Color", "Pink")
            Me._LineWidth = CInt(objRegistry.GetValue("Line Width", 5))
            Me._RecordPositionEvery = CInt(objRegistry.GetValue("Record Position Every", 1))
            Me._Transparency = CInt(objRegistry.GetValue("Transparency", 50))
            Me._SpeedAlert = CInt(objRegistry.GetValue("Speed Alert", 50))
            Me._SpeedAlert2 = CInt(objRegistry.GetValue("Speed Alert2", 80))
            Me._LitrePer100KM = CDbl(objRegistry.GetValue("Litre Per 100 KM", 8))
            Me._PriceOf20Litres = CDbl(objRegistry.GetValue("Price Of 20 Litres", 33000))
            Me._SoundLanguage = objRegistry.GetValue("Sound Language", "English")
            Me._Unit = objRegistry.GetValue("Unit", "Metric")
            Me._Currency = objRegistry.GetValue("Currency", "$")
            Me._EnableAlerts = CBool(objRegistry.GetValue("Enable Alerts", "True"))
            Me.Save()
        Catch ex As Exception
            PushLog("Error in clsMobileSettings.Load: " & ex.Message)
        End Try
    End Sub

    Public Sub New()
        Me.Load()
    End Sub
End Class
